package com.china.lottery.listener.task.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.china.lottery.listener.bean.AccountTrans;
import com.china.lottery.listener.code.PayIncrementCode;
import com.china.lottery.listener.code.PrizeDecrementCode;
import com.china.lottery.listener.dao.AgentConfigDao;
import com.china.lottery.listener.dao.DictionaryDao;
import com.china.lottery.listener.dao.PayAccountDao;
import com.china.lottery.listener.dao.PrizeAccountDao;
import com.china.lottery.listener.dao.entity.LAgentConfig;
import com.china.lottery.listener.dao.entity.LPayAccount;
import com.china.lottery.listener.dao.entity.LPayAccountHis;
import com.china.lottery.listener.dao.entity.LPrizeAccount;
import com.china.lottery.listener.dao.entity.LPrizeAccountHis;
import com.china.lottery.listener.task.core.BaseNoteService;
import com.china.lottery.listener.util.Constant;
import com.china.lottery.listener.util.HttpClient3;
import com.china.lottery.listener.util.Tools;


@Component("autoBonusToHandselService")
public class AutoBonusToHandselService extends BaseNoteService{
	
	@Autowired
	private AgentConfigDao agentConfigDao;
	
	@Autowired
	private DictionaryDao dicDao;
	
	@Autowired
	private PrizeAccountDao prizeAccountDao;
	
	@Autowired
	private PayAccountDao payAccountDao;
	@Autowired
	private static String NUMCODE = Constant.NOTEINIT;
	
	@Override
	public List<String> getNumbers() {
		return this.getNumberFromParent(dicDao.findByParentCode(NUMCODE));
	}

	@Override
	public String getContent() {
		
		log.info("  start AutoBonusToHandselService query ");
		try {
			List<LAgentConfig> lacList = agentConfigDao.queryNeedAutoAgent();
			if(lacList != null){
				log.info(" need auto bouns agent num is :"+lacList.size());
				for (int i = 0; i < lacList.size(); i++) {
					LAgentConfig lcf =	lacList.get(i);
					LPrizeAccount pa= prizeAccountDao.findAccountByAgentId(lcf.getAgentId());
					if(pa != null){
						//奖金转彩金
						if(pa.getBalance() > 0){
							/**
							 * 奖金汇总
							 */
							Map<String, String> params = new HashMap<String, String>();
							params.put("agentId", pa.getAgentId());
							params.put("batchId", pa.getLastHisBatchId());
							params.put("money", String.valueOf(-pa.getBalance()));
							HttpClient3 http = new HttpClient3();
							String message = null;
							try {
								message = http.doPost("http://172.19.247.236:8048/LotteryAccount/account!prizeBatchSubmit", params);
								
								AccountTrans req = new AccountTrans(pa.getAgentId(),pa.getBalance(),"admin", "奖金自动转彩金");
								StringBuffer batchId = new StringBuffer();
								batchId.append("TRANS");
								batchId.append("_");
								batchId.append(Tools.getTimestamptoString(new Date()));
								batchId.append("_");
								batchId.append(req.getMoney());
								LPrizeAccountHis debitHis = LPrizeAccountHis.getCommit(pa, pa.getBalance(), PrizeDecrementCode.CODE_2303, 0l,batchId.toString());
								debitHis.setCommitStatus(LPayAccountHis.COMMIT_STATUS_YES);
								setPrizeAccountHisData(debitHis, req.getCreateUser(),req.getRemark());
								prizeAccountDao.save(debitHis);
								
								this.transfer(req);
								log.info("do post PrizeAwardDistribution agent:{}, batchId:{},response:{}",  pa.getAgentId(), pa.getLastHisBatchId(), message);
							} catch (Exception e) {
								log.error("do post PrizeAwardDistribution agent:{},batchId:{},error:{}", pa.getAgentId(), pa.getLastHisBatchId(), e.getMessage());
							}
						}
					}else{
						log.info("LPrizeAccount is null this agentId:"+lcf.getAgentId());
					}
					log.info(" agentId :"+lcf.getAgentId()+",auto bounds money:"+pa.getBalance());
				}
			}else{
				log.info("nobody agent need auto bonus this job end!");
			}
		} catch (Exception e) {
			e.printStackTrace();
			log.error("prizeTransAccount agent:{},e rror:{}",e.getMessage());
		}
		
		String content = "";
		return content;
	}
	
	
	private void setPrizeAccountHisData(LPrizeAccountHis his,String createUser,String remark) {
		if (!Tools.isBlank(createUser)) {
			his.setCreateUser(createUser);
		}
		if (!Tools.isBlank(remark)) {
			his.setRemark(remark);
		}
	}
	
	public void transfer(AccountTrans req) {

		String agentId = req.getAgentId();
		try {
			LPayAccount creditAccount = payAccountDao.findAccountByAgentId(agentId);
			LPayAccountHis creditHis = LPayAccountHis.getCommit(creditAccount, req.getMoney(), PayIncrementCode.CODE_1106, 0l);
			setPayAccountHisData(creditHis, req.getCreateUser(),req.getRemark());
			double payBalance =  payAccountDao.banlanceQuery(agentId);
			creditHis.setBalanceBefore(payBalance);   
			creditHis.setBalanceAfter(Tools.add(payBalance, creditHis.getDebitCredit()));
			payAccountDao.save(creditHis);
			
		}catch (Exception e){
			log.error("transfer accountServiceCodeException  exception :{} ",e.getMessage());
		}
	}
	
	private void setPayAccountHisData(LPayAccountHis his,String createUser,String remark) {
		if(!Tools.isBlank(createUser)) {
			his.setCreateUser(createUser);
		}
		if (!Tools.isBlank(remark)) {
			his.setRemark(remark);
		}
	}
	 
}
